# -*- coding: utf-8 -*-
# @Author : Yuan
from datetime import datetime

import os

from meiduo_mall import settings
from utils.goods import get_categories
from apps.contents.models import ContentCategory
from django.template import loader


def generate_static_index_html():
    """提供首页广告界面"""
    print('%s: generate_static_index_html' % datetime.now())
    # 查询商品频道和分类
    categories = get_categories()
    # 广告数据
    contents = {}
    content_categories = ContentCategory.objects.all()
    for cat in content_categories:
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

    # 渲染模板的上下文
    context = {
        'categories': categories,
        'contents': contents,
    }

    # 1、获取首页模板文件
    html_template = loader.get_template('index.html')
    # 2、渲染首页html字符串
    html_file = html_template.render(context)
    # 3、将首页html字符串写入到指定目录，命名'index.html'
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/index.html')
    with open(file_path, 'w') as f:
        f.write(html_file)

